package com.banma.common.utils;

import com.banma.common.entity.IpAddress;
import com.banma.common.entity.IpAddressVO;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 *  IP工具类
 *
 * <pre>
 * Pattern : Value Object
 * Thread Safe : No
 *
 * Change History
 *
 * Name                 Date                    Description
 * -------              -------                 -----------------
 * Banma              2016-11-16            Create the class
 *
 * </pre>
 *
 * @author Banma
 * @version 1.0
 */
public class IpUtils {


    public static String getAddressById(String ip) throws  Exception{
        URL url = new URL( "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String line = null;
        StringBuffer result = new StringBuffer();
        while((line = reader.readLine()) != null)
        {
            result.append(line);
        }
       // Gson gson = new Gson();
        //IpAddressVO ipAddressVO = gson.fromJson(result.toString(), IpAddressVO.class);
        //IpAddress ipAddress = ipAddressVO.getData();
        //System.out.print("address = 【"+ipAddress.getCountry()+ipAddress.getRegion()+ipAddress.getCity()+" "+ipAddress.getIsp()+"】");
        return result.toString();
    }

    //http://ip.taobao.com/service/getIpInfo.php?ip=119.75.218.70
}
